package io.other_stream;

import java.io.*;

/**
 * 用字节流向文件中写个整数1000 小数3.14 布尔true
 *
 * 注意： 写的顺序和读取的顺序要保持一致
 */
public class Demo_data {
    public static void main(String[] args) throws IOException {
        //extracted();

        //使用数据输出流写入java的基本数据类型
        //extracted1();

        //创建数据输入流读取数据
        //extracted2();


    }

    private static void extracted2() throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream("7-14.txt"));

        int i = dis.readInt();
        System.out.println("i = " + i);

        double d = dis.readDouble();
        System.out.println("d = " + d);

        boolean b = dis.readBoolean();
        System.out.println("b = " + b);

        dis.close();
    }

    private static void extracted1() throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("7-14.txt"));
        //写入1000
        dos.writeInt(1000);
        //写入double
        dos.writeDouble(3.14);
        //写入boolean
        dos.writeBoolean(true);

        dos.close();
    }

    private static void extracted() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("7-14.txt");

        /*
        write(int b)写入的是单个字节
        1000 --> 0000 0011 1110 1000
        write的常规协定是：向输出流写入一个字节
        要写入的字节是参数b的八个低位，b的24个高位将被忽略
         */
        fileOutputStream.write(1000);
    }

}
